package com.wsh.crawler.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wsh.crawler.constant.BusinessStatus;
import com.wsh.crawler.utils.MessageUtil;

import java.io.Serial;
import java.io.Serializable;

/**
 * 响应信息主体
 */
public class R<T> implements Serializable
{
    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public static final int SUCCESS = BusinessStatus.SUCCESS.getCode();

    /**
     * 失败
     */
    public static final int FAIL = BusinessStatus.FAIL.getCode();

    /**
     * 参数错误
     **/
    public static final int PARAM_ERROR = BusinessStatus.PARAM_ERROR.getCode();

    private int code;

    private String msg;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public static <T> R<T> ok() {
        return restResult(null, SUCCESS, MessageUtil.getMessage("common.successMsg"));
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, SUCCESS, MessageUtil.getMessage("common.successMsg"));
    }

    public static <T> R<T> ok(T data, String msg)
    {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> R<T> fail()
    {
        return restResult(null, FAIL, MessageUtil.getMessage("common.failureMsg"));
    }

    public static <T> R<T> fail(String msg)
    {
        return restResult(null, FAIL, msg);
    }

    public static <T> R<T> fail(T data)
    {
        return restResult(data, FAIL, MessageUtil.getMessage("common.failureMsg"));
    }

    public static <T> R<T> fail(T data, String msg) {
        return restResult(data, FAIL, msg);
    }

    public static <T> R<T> fail(int code, String msg) {
        return restResult(null, code, msg);
    }

    // 参数错误
    public static <T> R<T> paramErr(String msg) {
        return restResult(null, PARAM_ERROR, msg);
    }

    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setMsg(msg);
        apiResult.setData(data);
        return apiResult;
    }

    public int getCode()
    {
        return code;
    }

    public void setCode(int code)
    {
        this.code = code;
    }

    public String getMsg()
    {
        return msg;
    }

    public void setMsg(String msg)
    {
        this.msg = msg;
    }

    public T getData()
    {
        return data;
    }

    public void setData(T data)
    {
        this.data = data;
    }

    public static <T> Boolean isError(R<T> ret)
    {
        return !isSuccess(ret);
    }

    public static <T> Boolean isSuccess(R<T> ret)
    {
        return R.SUCCESS == ret.getCode();
    }

    // 将R对象转为json字符串
    public String toJsonString() {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.writeValueAsString(this);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
